package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.entity.CaClass;
import com.dingreading.cloud.soms.entity.table.CaClassTableDef;
import com.dingreading.cloud.soms.mapper.CaClassMapper;
import com.dingreading.cloud.soms.service.CaClassService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.dingreading.cloud.soms.entity.table.CaClassMemberTableDef.caClassMember;
import static com.dingreading.cloud.soms.entity.table.MembersTableDef.members;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CaClassServiceImpl extends BaseServiceImpl<CaClassMapper, CaClass, CaClassTableDef> implements CaClassService {

    @Override
    protected CaClassTableDef getTable() {
        return CaClassTableDef.caClass;
    }

    @Override
    public Page<CaClass> pageList(
            PageUtil pageUtil, String storeUid, String keyword, Integer freeStatus, Integer enabled, String projectUid, String teacherUid
    ) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.className.like(keyword));
        if (null != freeStatus)
            condition.and(table.freeStatus.eq(freeStatus));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));
        if (StringUtils.isNotBlank(projectUid))
            condition.and(table.projectUid.eq(projectUid));
        if (StringUtils.isNotBlank(teacherUid))
            condition.and(table.teacherUid.eq(teacherUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.enabled.desc(), table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public List<CaClass> getByStoreUid(String storeUid, String keyword, Integer enabled) {
        QueryCondition condition = table.storeUid.eq(storeUid);
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.className.like(keyword));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc(), table.closeStatus.desc(), table.enabled.desc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public CaClass getClassInfo(String storeUid, Long id) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.eq(id));

        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public boolean haveRoomId(String storeUid, Long roomId) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.roomId.eq(roomId));

        return exists(condition);
    }

    @Override
    public int dropBean(String storeUid, Long id) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.eq(id));

        return getMapper().deleteByCondition(condition);
    }

    @Override
    public int updateEnabled(String storeUid, Long id, Integer enabled, String staffUid, String staffName, Date time) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.eq(id));

        CaClass data = UpdateEntity.of(CaClass.class);
        UpdateWrapper<CaClass> wrapper = UpdateWrapper.of(data);
        wrapper.set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName));

        return getMapper().updateByCondition(data, condition);
    }

    @Override
    public int setClose(String storeUid, Long id, String staffUid, String staffName, Date time) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.eq(id));

        CaClass data = UpdateEntity.of(CaClass.class);
        UpdateWrapper<CaClass> wrapper = UpdateWrapper.of(data);
        wrapper.set(table.closeStatus, 0)
                .set(table.closeTime, time)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName));

        return getMapper().updateByCondition(data, condition);
    }

    @Override
    public int updateStudentNum(String storeUid, Long id, String staffUid, String staffName, Integer studentNum) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.eq(id));

        CaClass data = UpdateEntity.of(CaClass.class);
        UpdateWrapper<CaClass> wrapper = UpdateWrapper.of(data);
        wrapper.set(table.studentNum, studentNum)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName));

        return getMapper().updateByCondition(data, condition);
    }

    @Override
    public Page<CaClass> getInsertCassV2(PageUtil pageUtil, String storeUid, Long classId, String projectUid, String stageUid, String keyword) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.id.ne(classId))
                .and(table.projectUid.eq(projectUid))
                .and(table.stageUid.eq(stageUid))
                .and(table.enabled.eq(1))
                .and(table.closeStatus.eq(1));

        if (StringUtils.isNotBlank(keyword)) {
            QueryCondition queryCondition = members.storeUid.eq(storeUid)
                    .and(members.fullName.like(keyword).or(members.nickName.like(keyword)))
                    .and(members.enabled.eq(1));
            QueryWrapper queryMemberUids = QueryWrapper.create()
                    .select(QueryMethods.distinct(members.uid))
                    .from(members)
                    .where(queryCondition);
            QueryWrapper queryClassIds = QueryWrapper.create()
                    .select(caClassMember.classId)
                    .from(caClassMember)
                    .where(caClassMember.memberUid.in(queryMemberUids));

            condition.and(table.teacherName.like(keyword).or(table.id.in(queryClassIds)));
        }

        QueryWrapper wrapper = QueryWrapper.create()
                .select()
                .from(table)
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public void updateScheduleNum(String corpUid, String storeUid, Long classId, int i) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.corpUid.eq(corpUid))
                .and(table.id.eq(classId))
                .and(table.closeStatus.eq(1));

        CaClass data = UpdateEntity.of(CaClass.class);
        UpdateWrapper<CaClass> wrapper = UpdateWrapper.of(data);
        wrapper.setRaw(table.scheduleNum, "schedule_num + 1");

        getMapper().updateByCondition(data, condition);
    }

    @Override
    public boolean haveProjectUid(String storeUid, String projectUid) {
        QueryCondition condition = table.projectUid.eq(projectUid);
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));

        return exists(condition);
    }

    @Override
    public boolean haveStageUid(String storeUid, String stageUid) {
        QueryCondition condition = table.stageUid.eq(stageUid);
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));

        return exists(condition);
    }

    @Override
    public boolean updateInfo(
            Long id, String className, Long roomId, String roomName, String teacherUid, String teacherName,
            String stageUid, String stageName, String remark, String staffUid, String staffName
    ) {
        return UpdateChain.of(CaClass.class)
                .set(table.className, className)
                .set(table.roomId, roomId)
                .set(table.roomName, roomName)
                .set(table.teacherUid, teacherUid)
                .set(table.teacherName, teacherName)
                .set(table.stageUid, stageUid)
                .set(table.stageName, stageName)
                .set(table.remark, remark)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<CaClass> listByUidIn(String storeUid, List<String> classUidList) {
        return getMapper().selectListByCondition(table.storeUid.eq(storeUid).and(table.uid.in(classUidList)));
    }

}
